﻿using MediatR;
using WMS.API.Application.Command.InventoryManagement;
using WMS.Domain.Entities.InventoryManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.InventoryManagement
{
    /// <summary>
    /// 库存管理-箱子型号管理删除命令-grx
    /// </summary>
    public class DeleteBoxCommandHandler : IRequestHandler<DeleteBoxCommand, APIResult<int>>
    {
        private readonly IBaseRepository<BoxManagement> _boxRepository;

        public DeleteBoxCommandHandler(IBaseRepository<BoxManagement> boxRepository)
        {
            _boxRepository = boxRepository;
        }
        /// <summary>
        /// 删除箱子型号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  async Task<APIResult<int>> Handle(DeleteBoxCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>("");
            var box =await _boxRepository.FindAsync(request.Id);
            if (box == null)
            {
                result.Code = ResultCode.Failed;
                result.Message = "箱子型号不存在";
                result.Data = 0;
                return await Task.FromResult(result);
            }
             var i=await _boxRepository.DeleteAsync(request.Id);
            result.Code = ResultCode.Success;
            result.Message = i > 0 ? "删除箱子型号成功" : "删除箱子型号失败";
            return await Task.FromResult(result);
        }
    }
}
